package mo.leetcode_top;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class L56 {

    public int[][] merge(int[][] intervals) {

        List<int[]> ans = new ArrayList<>();
        Arrays.sort(intervals, (o1, o2) -> o1[0] == o2[0] ? o1[1] - o2[1] : o1[0] - o2[0]);
        int start = intervals[0][0];
        int end = intervals[0][1];
        for (int i = 1; i < intervals.length; i++) {
            if (end >= intervals[i][0]) {
                end = Math.max(end, intervals[i][1]);
            } else {
                ans.add(new int[]{start, end});
                start = intervals[i][0];
                end = intervals[i][1];
            }
        }
        ans.add(new int[]{start, end});
        return ans.toArray(new int[ans.size()][2]);
    }




    public static void main(String[] args) {
        L56 test = new L56();
        System.out.println(Arrays.deepToString(test.merge(new int[][]{{1, 4}, {0, 5}})));
    }
}
